// comps/uploader/cert.js
const App = getApp()
import Qiniu from '../../utils/qiniu'
import Util from '../../utils/util'
Component({
  /**
   * 组件的一些选项 
   */
  options: {
    addGlobalClass: true,
    multipleSlots: true,
  },
  /**
   * 组件的属性列表
   */
  properties: {
    type: {
      type: String,
      default: ''
    },
    src: {
      type: String,
      default: ''
    },
    width: {
      type: String,
      default: ''
    },
    height: {
      type: String,
      default: ''
    },
    readonly: {
      type: Boolean,
      default: false
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    init: false,
    imgs: {
      idca: 'https://cdn.hangzhouyizuke.com/res/app/icons/zjt/idca-u.png',
      idcb: 'https://cdn.hangzhouyizuke.com/res/app/icons/zjt/idcb-u.png',
      dlca: 'https://cdn.hangzhouyizuke.com/res/app/icons/zjt/dlca-u.png',
      dlcb: 'https://cdn.hangzhouyizuke.com/res/app/icons/zjt/dlcb-u.png',
      vlca: 'https://cdn.hangzhouyizuke.com/res/app/icons/zjt/vlca-u.png',
      vlcb: 'https://cdn.hangzhouyizuke.com/res/app/icons/zjt/vlcb-u.png',
      olc: 'https://cdn.hangzhouyizuke.com/res/app/icons/zjt/olca-u.png'
    },
    url: '',
    cancel: function () {},
    progress: {}
  },

  /**
   * 生命周期
   */
  lifetimes: {
    attached() {
      this.setData({
        init: true,
        url: this.data.src
      })
    },
    detached() {},
  },
  /**
   * 组件的方法列表
   */
  methods: {
    doDeleteImage() {
      this.setData({
        url: ''
      })
      let detail = {
        url: '',
        ocr: {}
      }
      this.triggerEvent('change', detail, {})
    },
    doPreviewImage() {
      wx.previewImage({
        urls: [this.data.url],
        current: this.data.url
      })
    },
    doUploadImage() {
      wx.chooseMedia({
        count: 1,
        mediaType: ['image'],
        sourceType: ['album', 'camera'],
        sizeType: ['compressed'],
        camera: 'back',
        success: (res) => {
          this.execUpload(res.tempFiles[0].tempFilePath)
        }
      })
    },
    execUpload(path) {
      App.doPost({
        uri: '/comm/tool/file/qiniu/info',
        body: {
          type: 'Image'
        },
        fnSucc: resp => {
          let key = resp.data.path + resp.data.fileid + '.' + path.split('.').pop()?.toLowerCase()
          Qiniu.upload(path, (res) => {
            this.setData({
              url: res.imageURL
            })
            let detail = {
              url: res.imageURL,
              ocr: {}
            }
            this.triggerEvent('change', detail, {})
            this.execOcr(res.imageURL)
          }, (err) => {
            console.log(err)
          }, {
            region: 'NCN',
            key: key,
            uptoken: resp.data.token,
            uptokenFunc: null,
            uptokenURL: null,
            domain: resp.data.domain,
            shouldUseQiniuFileName: false,
          }, (progress) => {
            this.setData({
              progress
            })
            // console.log('上传进度', progress.progress);
            // console.log('已经上传的数据长度', progress.totalBytesSent);
            // console.log('预期需要上传的数据总长度', progress.totalBytesExpectedToSend);
          }, (cancel) => {
            this.setData({
              cancel
            })
          })
        },
        fnFail: resp => {
          App.doAlert('出错啦', `上传失败，[${resp.stat.err}] ${resp.stat.msg}`)
        }
      })
    },
    execOcr(img) {
      let body = {
        url: img
      }
      if (this.data.type == 'idca') {
        body["type"] = 1
        body["front"] = true
      } else if (this.data.type == 'idcb') {
        body["type"] = 1
        body["front"] = false
      } else if (this.data.type == 'dlca') {
        body["type"] = 2
        body["front"] = true
      } else if (this.data.type == 'dlcb') {
        body["type"] = 2
        body["front"] = false
      } else if (this.data.type == 'vlca') {
        body["type"] = 3
        body["front"] = true
      } else if (this.data.type == 'vlcb') {
        body["type"] = 3
        body["front"] = false
      } else if (this.data.type == 'olc') {
        body["type"] = 4
        body["front"] = true
      }

      App.doPost({
        uri: '/comm/tool/cert/ocr',
        body: body,
        fnSucc: resp => {
          let detail = {
            url: img,
            ocr: resp.data[this.data.type]
          }
          this.triggerEvent('change', detail, {})
        },
        fnFail: () => {
          Util.Toast.fail('识别证件失败')
        }
      })
    },
  },

})